<template>
    <div class="register main">
        <h1 class="title">欢迎加入<span style="color: #42bd56;">笔记之家</span></h1>
        <form method="post" @submit.prevent="onSubmit()">
            <div class="form_username">
                <p class="text">用户名：<span class="tip tip_name">{{msgname}}</span></p>
                <label>
                    <input type="text" @chahge="checkname" ref="username" placeholder="用户名" name="username"
                        v-model.trim="username" />
                </label>
            </div>
            <div class="form_tel">
                <p class="text">手机号码 <span class="require">*</span>：<span class="tip tip_tel">{{msgtel}}</span></p>
                <label>
                    <input type="text" @blur="checktel" ref="tel" placeholder="手机号码" name="tel" v-model.trim="tel"
                        maxlength="11" />
                </label>
            </div>
            <div class="form_pwd">
                <p class="text">密码 <span class="require">*</span>：<span class="tip tip_pwd">{{msgpwd}}</span></p>
                <label>
                    <!-- <template v-if="passType === 'password'">
                        <input v-model.trim="pwd" type="password" @change="checkpwd" ref="pwd" name="pwd"
                            placeholder="密码">
                    </template>
                    <template v-if="passType === 'text'">
                        <input v-model.trim="pwd" type="text" @change="checkpwd" ref="pwd"  name="pwd" placeholder="密码">
                    </template> -->
                    <input v-model.trim="pwd" :type="passType" @blur="checkpwd" ref="pwd"  name="pwd" placeholder="密码">
                </label>
                <span class="show_pwd" :class="{show: passType == 'text'}" @click="showPwd()"></span>
            </div>
            <div class="form_pwdAgain">
                <p class="text">确认密码 <span class="require">*</span>：<span
                        class="tip tip_pwdAgain">{{msgpwdAgain}}</span></p>
                <label>
                    <!-- <template v-if="passType2 === 'password'">
                        <input v-model.trim="pwdAgain" type="password" @change="checkpwdAgain" ref="pwdAgain"  name="pwdAgain" placeholder="确认密码">
                    </template>
                    <template v-if="passType2 === 'text'">
                        <input v-model.trim="pwdAgain" type="text" @change="checkpwdAgain" ref="pwdAgain" name="pwdAgain" placeholder="确认密码">
                    </template> -->
                    <input v-model.trim="pwdAgain" :type="passType2" @blur="checkpwdAgain" ref="pwdAgain" name="pwdAgain" placeholder="确认密码">
                </label>
                <span class="show_pwd" :class="{show: passType2 == 'text'}" @click="showPwdAgain()"></span>
            </div>
            <router-link :to="{name:'login',query:{query:$route.query.query || ''}}" style="color:#42bd56;font-weight:700">去登陆</router-link>
            <div class="form_submit">
                <button class="submit" type="submit" :disabled="isDisabled"
                    :class="{disabled: isDisabled}">{{registerState}}</button>
            </div>
        </form>
    </div>
</template>
<script>
    export default {
        name: "register",
        data() {
            return {
                isDisabled: false,
                //isShow: 0,
                //isShowAgain: 0,
                registerState: "立即注册",
                passType: 'password',
                passType2: "password",
                tel: '',
                pwd: '',
                pwdAgain: '',
                username: '',
                msgname: '用户名由数字、字母、下划线组成，长度6~12位，不能以数字开头',
                msgpwd: '密码由数字字母组成，长度6位',
                msgpwdAgain: '',
                msgtel: '',
                token: '',
                check: false,
            }
        },
        methods: {
            checkname: function () {
                let regName = /^[a-z_]{1}[a-zA-Z0-9_]{5,11}$/i;
                if (!regName.test(this.username)) {
                    this.msgname = "用户名格式错误";
                    this.check = false;
                } else {
                    this.msgname = "";
                    this.check = true;
                }
            },
            checktel: function () {
                let regTel = /^[1]{1}[34578]{1}[0-9]{9}$/;
                if (!this.tel) {
                    this.msgtel = "电话号码不能为空";
                    this.check = false;
                } else {
                    if (!regTel.test(this.tel)) {
                        this.msgtel = "电话号码格式错误";
                        this.check = false;
                    } else {
                        this.msgtel = "";
                        this.check = true;
                    }
                }
            },
            checkpwd: function () {
                let regPwd = /^[a-z0-9]{6}$/i;
                if (!this.pwd) {
                    this.msgpwd = "密码不能为空";
                    this.check = false;
                } else {
                    if (!regPwd.test(this.pwd)) {
                        this.msgpwd = "密码格式错误";
                        this.check = false;
                    } else {
                        this.msgpwd = "";
                        this.check = true;
                    }
                }
            },
            checkpwdAgain: function () {
                if (!this.pwdAgain) {
                    this.msgpwdAgain = "密码不能为空";
                    this.check = false;
                } else {
                    if (this.pwdAgain !== this.pwd) {
                        this.msgpwdAgain = "两次输入的密码不一致";
                        this.check = false;
                    } else {
                        this.msgpwd = "";
                        this.check = true;
                    }
                }
            },
            showPwd: function () {
                // this.isShow = this.isShow ? 0 : 1
                // this.isShow ? this.passType = 'text' : this.passType = 'password'
                this.passType =   this.passType == 'text' ?  'password' : "text";
            },
            showPwdAgain: function () {
               // this.isShowAgain = this.isShowAgain ? 0 : 1
               // this.isShowAgain ? this.passType2 = 'text' : this.passType2 = 'password'
                this.passType2 =   this.passType2 == 'text' ?  'password' : "text";
            },
            beforeSubmit: function () {
                this.isDisabled = true
                this.registerState = '正在提交...'
            },
            onSuccess: function (res) {
                this.$router.push({name:'login',query:{query:this.$route.query.query || ''}});
            },
            onError: function (err) {
                this.registerState = '立即注册'
                this.isDisabled = false
            },
            onSubmit: function () {
                if(!this.check){
                    return;
                }
                this.beforeSubmit() // Disabled submit button
                // Regist...
                let username = this.$refs.username.value;
                let pwdAgain = this.$refs.pwdAgain.value;
                let tel = this.$refs.tel.value;
                this.$api.register({
                    tel: tel,
                    password: pwdAgain,
                    name: username
                }).then(res => {
                    //console.log(res);
                    // Success handle
                    this.onSuccess(res)
                }, err => {
                    // Error handle
                    this.onError(err)
                })
            }
        }
    }
</script>
<style lang="scss" scoped>
    .require {
        color: red;
    }

    .main {
        width: 600px;
        margin: 0 auto;

        .title {
            width: 100%;
            height: 80px;
            line-height: 80px;
            text-align: center;
        }

        form {
            padding: 15px;

            div {
                margin-bottom: 15px;

                .text {
                    height: 20px;
                    font-size: 14px;
                    color: #aaa;
                    margin-bottom: 5px;

                    .tip {
                        color: red;
                    }

                }

                label {
                    display: block;
                    width: 100%;
                    height: 60px;

                    input::placeholder {
                        color: #ccc;
                    }

                    input {
                        width: 100%;
                        height: 100%;
                        padding: 0 10px;
                        box-sizing: border-box;
                        font-size: 1.5rem;
                        background: #fff;
                        border: 1px solid #ccc;
                        border-radius: 10px;
                        outline: 0;
                    }
                }
            }

            .form_pwd,
            .form_pwdAgain {
                position: relative;

                input {
                    padding-right: 80px;
                }

                .show_pwd {
                    position: absolute;
                    right: 0;
                    top: 25px;
                    width: 80px;
                    height: 60px;
                    background: url("../assets/pwd_hide.png") center center no-repeat;
                    background-size: 36px;
                }

                .show_pwd.show {
                    background: url("../assets/pwd_show.png") center center no-repeat;
                    background-size: 36px;
                }
            }

            .form_submit {
                .submit {
                    cursor: pointer;
                    width: 100%;
                    height: 4.4rem;
                    margin-top: 1rem;
                    border: 0.1rem solid #17AA52;
                    border-radius: 0.3rem;
                    width: 100%;
                    height: 100%;
                    font-size: 1.7rem;
                    line-height: 4.4rem;
                    text-align: center;
                    color: #fff;
                    background: #42bd56;
                }

                .disabled {
                    cursor: not-allowed;
                    background: #eee;
                    border: none;
                }
            }
        }
    }
</style>